///**
// *
// */
//package com.example.oneAgent;
//
//import java.io.IOException;
//import java.io.InputStream;
//import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
//import org.apache.commons.httpclient.HttpClient;
//import org.apache.commons.httpclient.HttpException;
//import org.apache.commons.httpclient.HttpMethodBase;
//import org.apache.commons.httpclient.HttpStatus;
//import org.apache.commons.httpclient.methods.GetMethod;
//import org.apache.commons.httpclient.params.HttpMethodParams;
//import com.itrade.aastock.errors.AAStockRequestParseException;
//import com.itrade.base.data.DataPadding;
//import com.itrade.base.data.ref.HashMapDataPadding;
//import com.itrade.base.jesapi.ITradeAPI;
//import com.itrade.configuration.diversity.BaseAppDiversityConfiguration;
//import com.itrade.util.Log;
//import com.itrade.web.helper.ITradeSessionManager;
//
///**
// * <p>Copyright: Copyright(c)Transaction Technologies Limited 2014-5-22 by xuejie.xiao</p>
// * @author jay.wince
// * @Date    2014-5-22
// * @version 1.0
// */
//public class DefaultAARequestParser implements IAARequestParser<String>{
//
//	private final String XML_ENCODING = "UTF-8";
//	private final String retryCount	  = BaseAppDiversityConfiguration.getInstance().getAAStockSnapshotDataFeedOptions().get("retry-count");
//	private final String HTTP_EXCEPTION = "HTTP_EXCEPTION";
//	private final String IO_EXCEPTION = "IO_EXCEPTION";
//	/* (non-Javadoc)
//	 * @see com.itrade.aastock.AARequestParser#requestParse(java.lang.String)
//	 */
//	@Override
//	public String requestParse(String url) throws AAStockRequestParseException{
//		HttpClient client = new HttpClient();
//// Create a method instance.
//	    GetMethod method = new GetMethod(url);
//
//// Provide custom retry handler is necessary
//	    int rc = 3;
//	    if (!(this.retryCount == null || this.retryCount.length() ==0)) {
//			rc = Integer.parseInt(this.retryCount);
//		}
//	    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(rc, false));
//        Throwable error = null;
////BEGIN TASK #:TTL-GZ-STEVENZG.LI-00019 2015-12-22[ITradeR5-CISI]Remove Notification,deal with the same exception.(ITRADEFIVE-710)
//	    try {
//	      // Execute the method.
//	      int statusCode = client.executeMethod(method);
//	      if (statusCode != HttpStatus.SC_OK) {
//	    	  Log.println("Method failed: " + method.getStatusLine(),Log.ERROR_LOG);
////Pending to implement:
//	      }
//          return getResponseText(method);
//	    } catch (HttpException e) {
//	        //Log.println("Fatal protocol violation: " + e.getMessage(),Log.ERROR_LOG);
//	        e.printStackTrace();
//	        error = e;
//	    } catch (IOException e) {
//	    	//Log.println("Fatal transport error: " + e.getMessage(),Log.ERROR_LOG);
//	        e.printStackTrace();
//	        error = e;
//	    } catch (Exception e) {
//	        e.printStackTrace();
//            error = e;
//	    }/* catch (DocumentException e) {
//	    	Log.println("Fatal document error: " + e.getMessage(),Log.ERROR_LOG);
//			e.printStackTrace();
//			error = e;
//		} */finally {
//		    // Release the connection.
//			method.releaseConnection();
//	    }
//	    //Deal with the exception
//	    Integer errorSession = null;
//	    if (error != null) {
//	        if (error instanceof HttpException) {
//	            errorSession = ITradeSessionManager.getSessionAttributeValue(HTTP_EXCEPTION);
//	            if (errorSession != null) {
//	                errorSession = errorSession + 1;
//	                ITradeSessionManager.storeValue(HTTP_EXCEPTION, errorSession);
//                    Log.println(this.getClass().getSimpleName() + "#requestParse(String url:)" + url + ",Raise Fatal protocol violation:" + error.getMessage(), Log.ERROR_LOG);
//	            } else {
//	                ITradeSessionManager.storeValue(HTTP_EXCEPTION, 1);
//	            }
//	        } else if (error instanceof IOException) {
//	            errorSession = ITradeSessionManager.getSessionAttributeValue(IO_EXCEPTION);
//	            if (errorSession != null) {
//	                errorSession = errorSession + 1;
//	                ITradeSessionManager.storeValue(IO_EXCEPTION, errorSession);
//	                Log.println(this.getClass().getSimpleName() + "#requestParse(String url:)" + url + ",Raise Fatal transport error:" + error.getMessage(), Log.ERROR_LOG);
//	            } else {
//	                ITradeSessionManager.storeValue(IO_EXCEPTION, 1);
//	            }
//	        }
//	    }
//	    if (errorSession == null) {
//    		DataPadding<Object> options = new HashMapDataPadding<Object>();
//    		options.put(com.itrade.tag.ExceptionOptionTag.AA_FULL_PATH_KEY, url);
//    //FiXME#: Pending to implement
//    /*
//    	    String lvRequestName = null;
//    	    options.put(com.itrade.tag.ExceptionOptionTag.AA_REQUEST_NAME, lvRequestName);
//    */
//    		ITradeAPI.exceptionHandler().handle(
//    				AAStockRequestParseException.class,
//    				this.getClass().getSimpleName()+"#requestParse(String url:)"+url,
//    				error.getMessage(),
//    				error,
//    				options
//    				);
//	    }
////END TASK #:TTL-GZ-STEVENZG.LI-00019 2015-12-22[ITradeR5-CISI]Remove Notification,deal with the same exception.(ITRADEFIVE-710)
//	    return null;
//	}
//
//	/**
//	 * Extract the response text from the http response.
//	 * @author jay.wince
//	 * @since  20140923
//	 * @param method
//	 * @return
//	 * @throws IOException
//	 */
//	private final String getResponseText(HttpMethodBase method) throws IOException{
//		 String lvResponseText = null;
//// Method 1: Read the response body as bytes.
////	      byte[] responseBody = method.getResponseBody();
////Deal with the response.
////Use caution: ensure correct character encoding and is not binary data
////	      System.out.println(new String(responseBody,"UTF-8"));
//
///*	      SAXReader reader = new SAXReader();
//		  reader.setEncoding(XML_ENCODING);
//		  reader.read(method.getResponseBodyAsStream());
//*/
////   	  return new String(responseBody,XML_ENCODING);
//
//// Method 2: Read the response body as stream.
//		java.util.Scanner lvScanner = null;
//		InputStream lvInputStream   = null;
//		try{
//			lvInputStream = method.getResponseBodyAsStream();
//			lvScanner     = new java.util.Scanner(lvInputStream,"UTF-8").useDelimiter("\\A");
//			if(lvScanner != null){
//				lvResponseText = lvScanner.hasNext() ? lvScanner.next() : null;
//			}
//		}finally {
//	           if(lvInputStream != null){
//	               try {
//	            	   lvInputStream.close();
//	            	   lvScanner.close();
//	               } catch(IOException e){
//	                   Log.println(e, Log.ERROR_LOG);
//	                   throw e;
//	               }
//	            }
//	    }
//		return lvResponseText;
//	}
//}
//
